import axios from 'axios'
import {Notify, Toast} from 'vant';
import router from "../router";



const request = axios.create({
    baseURL:'https://api.shop.eduwork.cn/',
    timeout:5000
})

// 请求拦截
 request.interceptors.request.use(config=>{
     const token = window.localStorage.getItem('token')
     if(token){
        config.headers.Authorization = 'Bearer ' + token
         return config
     }
     // console.log(config);
     return config
 },error => {
     return error
 })
//  相应拦截

 request.interceptors.response.use(res =>{
     // // console.log(res);
     // if(!res){
     //     Toast.loading({
     //         message: '加载中...',
     //         forbidClick: true,
     //     });
     // }else {
         return res

     // }

 },error => {
     // 如果有需要授权才能访问的接口，统一去login处理
     if(error.response.status === 401){
         Toast('请先登录!')
         router.push({path:'/login'})
     }
     if(error.response.status === 400){
         Toast(error.response.data.message)
         // console.log(error)
     }
     console.log();
     Notify(error.response.data.errors[Object.keys(error.response.data.errors)[0]][0])

 })


 
export default request
